// const puppeteer = require('puppeteer-extra');
// const StealthPlugin = require('puppeteer-extra-plugin-stealth');
const puppeteer = require('puppeteer');
// puppeteer.use(StealthPlugin());

async function getContent(url) {
    let browser;
    try {
        browser = await puppeteer.launch({
            headless: 'new',
            // headless: false,
            args: [
                '--no-sandbox',
                '--disable-setuid-sandbox',
                '--disable-blink-features=AutomationControlled'
            ],
            defaultViewport: { width: 1280, height: 720 }
        });

        const page = await browser.newPage();
        await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0');

        await page.goto(url, {
            waitUntil: 'networkidle2',
            timeout: 30000
        });

        await page.waitForFunction(
            () => document.body.innerText.includes('<rss version="2.0"'),
            { timeout: 20000 }
        );

        // 5. 获取页面内容（即RSS的XML字符串）
        const rssContent = await page.content();
        return rssContent;

    } catch (error) {
        console.error('获取RSS内容失败:', error);
        return null;
    } finally {
        // 6. 关闭浏览器（避免内存泄漏）
        if (browser) {
            await browser.close();
        }
    }
}

module.exports = { getContent };